<?php


declare(strict_types=1);

namespace app\operations\controller\admin;

use app\operations\model\Brand as BrandModel;
use app\operations\model\Store as StoreModel;
use sheep\controller\Backend;
use think\facade\Db;
use think\Request;
use think\Response;

//品牌申请
class BrandAudit extends Backend
{

    protected $model = null;
    protected $storeModel = null;


    protected function initialize()
    {
        $this->model = new BrandModel;
        $this->storeModel = new StoreModel;
    }


    /**
     * 查看
     *
     * @return Response
     */
    public function list(Request $request): Response
    {

        $list = $this->model->sheepFilter()->paginate(request()->param('list_rows', 10));

        foreach($list as &$val){
            $val['store'] = $this->storeModel->where('organ_id',$val['organ_id'])->where('type',$val['type'])->find();
            $val['category_text'] = $this->model->getCategoryNamesAttr(null, $val);
        }

        return success('获取成功', $list);

    }


    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $data = $this->model->findOrFail($id);
        $data['category_text'] = $this->model->getCategoryNamesAttr(null, $data);
        $data['store'] = $this->storeModel->where('organ_id',$data['organ_id'])->where('type',$data['type'])->find();
        return success('获取成功', $data);
    }


    //审核
    public function audit($id){
        $id = intval($id);
        $params = $this->request->only(['status', 'remarks']);
        if(in_array($params['status'],[1,2])) {
            Db::transaction(function () use ($id,$params) {
                $apply = $this->model->where('id',$id)->find();
                $apply->audit_status = $params['status'] ?? '0';
                $apply->remarks = $params['remarks'] ?? '';
                $apply->save();
            });

            return success('审核成功');
        }
        return success('审核成功',$id);

    }






}
